
package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.MaterialSpec;
import com.scs.application.modules.base.mapper.MaterialSpecMapper;
import com.scs.application.modules.base.request.MaterialSpecQueryRequest;
import com.scs.application.modules.base.service.MaterialSpecService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 商品规格 service 实现
 */
@Slf4j
@Service
public class MaterialSpecServiceImpl extends BaseServiceImpl<MaterialSpecMapper, MaterialSpec> implements MaterialSpecService {

    @Override
    public IPage<MaterialSpec> page(MaterialSpecQueryRequest request) {
        if (UserUtils.isSupplier()) {
            request.setSupplierId(UserUtils.getSupplierId());
        }
        IPage page = baseMapper.page(request.getPage(), request);
        return page;
    }

    @Override
    public List<MaterialSpec> list(MaterialSpecQueryRequest request) {
        return this.baseMapper.list(request);
    }

    @Override
    public MaterialSpec findMaterialSpecByName(String materialId, String materialSpec, Double price) {
        return this.getOne(Wrappers.<MaterialSpec>query()
                .eq(StringUtils.isNotBlank(materialId), "material_id", materialId)
                .eq(StringUtils.isNotBlank(materialSpec), "material_spec", materialSpec)
                .eq(StringUtils.isNotEmpty(materialSpec), "price", price)
                .last("limit 1")
        );
    }

    /**
     * 批量保存品类
     */
    @Override
    public int insertBatchSomeColumn(List<MaterialSpec> entityList) {
        return this.baseMapper.insertBatchSomeColumn(entityList);
    }

}
